package com.leff.cards.request;

import com.leff.cards.Dictionary;
import com.leff.cards.StoreWrapper;
import com.leff.cards.entity.Group;
import com.leff.cards.entity.User;

public class LeaveGroup extends Request {
	
	public LeaveGroup(Dictionary dict) {
		super(dict);
	}
	
	public RequestResult doLeave() {
		
		RequestResult result = this.verifyRequiredParameters(Parameter.GROUP_ID, Parameter.ID);
		if(!result.isSuccessful()) {
			return result;
		}
		
		long groupId = Long.valueOf((String)mDictionary.get(Parameter.GROUP_ID));
		Group G = StoreWrapper.getGroupById(groupId);
		if(G == null) {
			result.setErrorResult("No group found with ID " + groupId);
			return result;
		}
		
		long userId = Long.valueOf((String)mDictionary.get(Parameter.ID));
		User U = StoreWrapper.getUserById(userId);
		if(U == null) {
			result.setErrorResult("No user found with ID " + userId);
			return result;
		}
		
		if(G.removeMember(U)) {
			StoreWrapper.store(G);
		}
		
		result.setSuccessMessage("Group Left");
		
		return result;
	}
}
